<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    let movies = []
    // 排序
    const sorted = movies.sort((a, b) => {
        const aEmpty = !a.total || a.total.length === 0;
        const bEmpty = !b.total || b.total.length === 0;
        if (aEmpty && !bEmpty) return 1;
        if (!aEmpty && bEmpty) return -1;
        return a.movieName.localeCompare(b.movieName, 'zh-Hans-CN');
    });
    // 提取观影地址和陪同人
    console.log(JSON.stringify(sorted, null, 2));
    function extractUniqueSorted (movies) {
        const accompanySet = new Set();
        const addressSet = new Set();

        movies.forEach(movie => {
            (movie.total || []).forEach(item => {
                // 提取 address
                if (item.address && item.address.trim()) {
                    addressSet.add(item.address.trim());
                }

                // 提取 accompany
                if (Array.isArray(item.accompany)) {
                    item.accompany.forEach(acc => {
                        if (acc && acc.trim()) {
                            accompanySet.add(acc.trim());
                        }
                    });
                }
            });
        });

        // 转为数组、去重、排序（中文拼音或英文顺序）
        const collator = new Intl.Collator('zh-Hans-CN');
        const result = {
            accompany: Array.from(accompanySet).sort(collator.compare),
            address: Array.from(addressSet).sort(collator.compare)
        };

        return result;
    }

    console.log(extractUniqueSorted(movies));

    function findDuplicateMovies (movies) {
        const map = new Map();

        // 按 movieName 分组
        movies.forEach(movie => {
            const name = movie.movieName;
            if (!map.has(name)) map.set(name, []);
            map.get(name).push(movie);
        });

        // 只保留重名的部分
        const duplicates = [];
        for (const [name, list] of map.entries()) {
            if (list.length > 1) {
                duplicates.push({
                    movieName: name,
                    count: list.length,
                    items: list
                });
            }
        }

        return duplicates;
    }

    // ✅ 调用示例
    console.log(JSON.stringify(findDuplicateMovies(movies), null, 2));

</script>

</html>